package com.huiqiao.bus.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huiqiao.bus.domain.Car;
import com.huiqiao.bus.mapper.CarMapper;
import com.huiqiao.bus.service.ICarService;
import com.huiqiao.bus.vo.CarVo;
import com.huiqiao.sys.utils.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @Author: yanliwei@foolish
 * @CreateTime: 2024-12-16  14:33
 */

@Service
@Transactional //事务控制
public class CarServiceImpl implements ICarService {

    @Autowired
    private CarMapper carMapper;


    @Override
    public DataGridView queryAllCar(CarVo carVo) {
        Page<Object> page = PageHelper.startPage(carVo.getPage(), carVo.getLimit());

        Weekend<Car> weekend = Weekend.of(Car.class);
        WeekendCriteria<Car, Object> carObjectWeekendCriteria = weekend.weekendCriteria();
        if(StrUtil.isNotBlank(carVo.getCarnumber())){
            carObjectWeekendCriteria.andLike(Car::getCarnumber, "%" + carVo.getCarnumber() + "%");
        }
        if (StrUtil.isNotBlank(carVo.getCartype())){
            carObjectWeekendCriteria.andLike(Car::getCartype, "%" + carVo.getCartype() + "%");
        }
        if(StrUtil.isNotBlank(carVo.getColor())){
            carObjectWeekendCriteria.andLike(Car::getColor, "%" + carVo.getColor() + "%");
        }
        if (StrUtil.isNotBlank(carVo.getDescription())){
            carObjectWeekendCriteria.andLike(Car::getDescription, "%" + carVo.getDescription() + "%");
        }
        if(Objects.nonNull(carVo.getIsrenting())){
            carObjectWeekendCriteria.andEqualTo(Car::getIsrenting, carVo.getIsrenting());
        }

        List<Car> data = carMapper.selectByExample(weekend);

        return new DataGridView(page.getTotal(), data);
    }

    @Override
    public void addCustomer(CarVo carVo) {
        carMapper.insertSelective(carVo);
    }

    @Override
    public void deleteCustomer(String carnumber) {
        carMapper.deleteByPrimaryKey(carnumber);
    }

    @Override
    public void updateCustomer(CarVo carVo) {
        carMapper.updateByPrimaryKeySelective(carVo);
    }

    @Override
    public void deleteBatchCustomer(String[] ids) {
        Arrays.stream(ids).forEach(id -> carMapper.deleteByPrimaryKey(id));
    }

    @Override
    public Car queryCustomerByCarNumber(String carnumber) {
        return carMapper.selectByPrimaryKey(carnumber);
    }

    @Override
    public int updateCar(CarVo carVo) {
        try {
            this.carMapper.updateByPrimaryKeySelective(carVo);
            return 0;
        }catch (Exception e){
            System.out.println("e = " + e);
            return -1;
        }
    }
}
